package flag

import (
	sys_flag "flag"
	"github.com/fatih/structs"
	"giv-vue-blog/core"
	"giv-vue-blog/global"
)

// Option 命令行参数
type Option struct {
	DB   bool   //命令行迁移表结构
	User string //命令行创建用户 -u admin -u user
	Es   string // -es create -es delete
}

func Parse() Option {
	db := sys_flag.Bool("db", false, "初始化数据库")
	user := sys_flag.String("u", "", "创建用户")
	es := sys_flag.String("es", "", "es操作")
	//	解析命令行参数写入注册的flag里
	sys_flag.Parse()
	return Option{
		DB:   *db,
		User: *user,
		Es:   *es,
	}
}
func IsWebStop(option Option) (f bool) {
	//if option.DB {
	//	return true
	//}
	//return false
	maps := structs.Map(&option) //结构体转map
	for _, value := range maps {
		switch val := value.(type) {
		case string:
			if val != "" {
				f = true
			}
		case bool:
			if val == true {
				f = true
			}
		}
	}
	return f
}
func SwitchOption(option Option) {
	if option.DB {
		MakeMigrations()
		return
	}
	if option.User == "admin" || option.User == "user" {
		CreateUser(option.User)
		return
	}
	if option.Es == "create" {
		global.EsClient = core.EsConnect()
		EsCreateIndex()
	}
	//如果输入错误 走这个函数
	//sys_flag.Usage()
}
